import GameRecord from "../platform/GameRecord";

interface FuncInterMapStruct {
    target: any;
    value: string;
    lastTime: number;
}
interface FuncNameMapStruct {
    name : string;
    lastTime: number;
}
export default class Decorator {

    static FuncNameMap : FuncNameMapStruct[] = []
    static FuncInterMap: FuncInterMapStruct[] = []
    static DoubleClickMap: FuncInterMapStruct[] = []
    static DoubleNameClickMap: FuncNameMapStruct[] = []

    /**调用set和get方法时打印 */
    static logProperty(target: any, key: string) {

        // 属性值
        var _val = this[key];

        // getter
        var getter = function () {
            console.log(`Get: ${key} => ${_val}`);
            return _val;
        };

        // setter
        var setter = function (newVal) {
            console.log(`Set: ${key} => ${newVal}`);
            _val = newVal;
        };

        // 删除属性
        if (delete this[key]) {
            // 创建新的属性
            Object.defineProperty(target, key, {
                get: getter,
                set: setter,
                enumerable: true,
                configurable: true
            });
        }
    }
    /**播放视频 */
    static ShowVideo(...data : string[]){
        Log('传入参数长度:',data.length)
        return function(target,key,descriptor){
            let oldFunc = descriptor.value;
            if (data.length == 0){
                descriptor.value = function(...args){
                    GameRecord.showVideo((succ)=>{
                        if (succ){
                            oldFunc.apply(this, args);
                        }
                    })
                }
            }
            else{
                descriptor.value = function(...args){
                    if (data.indexOf(args[1]) >= 0){
                        GameRecord.showVideo((succ)=>{
                            if (succ){
                                oldFunc.apply(this, args);
                            }
                        })
                    }
                    else{
                        oldFunc.apply(this,args);
                    }
                }
            }
        }
    }
    /**分享录屏 */
    static ShareRecord(data?:string){
        return function(target,key,descriptor){
            let oldFunc = descriptor.value;
            if (!data){
                descriptor.value = function(...args){
                    GameRecord.shareRecord((succ)=>{
                        oldFunc.apply(this, args);
                    })
                }
            }
            else{
                descriptor.value = function(...args){
                    if (args[1] == data){
                        GameRecord.shareRecord((succ)=>{
                            oldFunc.apply(this, args);
                        })
                    }
                    else{
                        oldFunc.apply(this,args);
                    }
                }
            }
        }
    }

    // static logFunc(target: any, name: string, descriptor: PropertyDescriptor);
    static logFunc(param = '') {
        return function (target: any, name: string, descriptor: PropertyDescriptor) {
            Log(param + ' 函数名:', name)
        }
    }
    /**
     * 按钮回调的触发事件间隔,根据btncallback里的data单独延时,需要传入event|target,data(可见btnCallback)
     * @param delayTime 单位:秒
     */
    static FunctionIntervalDelay(delayTime: number = 0.5) {
        Log('设置延迟')
        return function (targFunc: any, name: string, descriptor: PropertyDescriptor) {
            let oldFunc = descriptor.value;
            descriptor.value = function (...args) {
                let target: cc.Node = args[0].target;
                let value = args[1];
                let time = new Date().getTime();

                let nullObj = null;
                for (let obj of Decorator.FuncInterMap) {
                    if (obj.target.name == '') {
                        if (nullObj == null) {
                            nullObj = obj;
                        }
                        continue;
                    }
                    if (obj.target == target) {
                        let hm = time - obj.lastTime;
                        if (hm / 1000 >= delayTime) {
                            obj.lastTime = time;
                            oldFunc.apply(this, args);
                            return;
                        }
                        else {
                            Log('------间隔中拒绝------')
                            return;
                        }
                    }
                }
                if (nullObj) {
                    nullObj.target = target;
                    nullObj.value = value;
                    nullObj.lastTime = time;
                    Log('--------回收一个对象-------')
                }
                else {

                    let info = {
                        'target': target,
                        'value': value,
                        'lastTime': time
                    }
                    Decorator.FuncInterMap.push(info);
                }
                oldFunc.apply(this, args)
            }
        }
    }
    /**
     * 按钮回调的触发事件间隔,根据函数名单独延时,需要传入event|target,data(可见btnCallback)
     * @param delayTime 单位:秒
     */
    static FunctionNameDelay(delayTime: number = 0.5) {
        Log('设置延迟')
        return function (targFunc: any, funcName: string, descriptor: PropertyDescriptor) {
            let oldFunc = descriptor.value;
            descriptor.value = function (...args) {
                let time = new Date().getTime();

                let nullObj = null;
                for (let obj of Decorator.FuncNameMap) {
                    if (obj.name == funcName) {
                        nullObj = obj;
                        let hm = time - obj.lastTime;
                        if (hm / 1000 >= delayTime) {
                            obj.lastTime = time;
                            oldFunc.apply(this, args);
                            return;
                        }
                        else {
                            Log('------间隔中拒绝------')
                            return;
                        }
                    }
                }
                if (nullObj) {
                    nullObj.name = funcName;
                    nullObj.lastTime = time;
                    Log('--------回收一个对象-------')
                }
                else {
                    let info = {
                        'name': funcName,
                        'lastTime': time
                    }
                    Decorator.FuncNameMap.push(info);
                }
                oldFunc.apply(this, args)
            }
        }
    }
    /**
     * 双击触发函数，依然需要传入event和data
     * @param intervalTime 双击判断最大时间间隔
     */
    static DoubleClickEventByData(intervalTime: number = 0.5) {
        return function (targFunc: any, name: string, descriptor: PropertyDescriptor) {
            let oldFunc = descriptor.value;
            descriptor.value = function (...args) {
                let target: cc.Node = args[0].target;
                let value = args[1];
                let time = new Date().getTime();

                let nullObj = null;
                for (let obj of Decorator.DoubleClickMap) {
                    if (obj.target.name == '') {
                        if (nullObj == null) {
                            nullObj = obj;
                        }
                        continue;
                    }
                    if (obj.target == target) {
                        let hm = time - obj.lastTime;
                        if (hm / 1000 <= intervalTime) {
                            let index = Decorator.DoubleClickMap.indexOf(obj);
                            Decorator.DoubleClickMap.splice(index, 1);
                            oldFunc.apply(this, args)
                            return;
                        }
                        else {
                            obj.lastTime = time;
                            return;
                        }
                    }
                }
                if (nullObj) {
                    nullObj.target = target;
                    nullObj.value = value;
                    nullObj.lastTime = time;
                }
                else {

                    let info = {
                        'target': target,
                        'value': value,
                        'lastTime': time
                    }
                    Decorator.DoubleClickMap.push(info);
                }
            }
        }
    }
    /**
     * 双击触发函数，根据函数名判断
     * @param intervalTime 双击判断最大时间间隔
     */
    static DoubleClickEventByFunctionName(intervalTime: number = 0.5) {
        return function (targFunc: any, funcName: string, descriptor: PropertyDescriptor) {
            let oldFunc = descriptor.value;
            descriptor.value = function (...args) {
                let time = new Date().getTime();

                let nullObj = null;
                for (let obj of Decorator.DoubleNameClickMap) {
                    if (obj.name == funcName) {
                        let hm = time - obj.lastTime;
                        if (hm / 1000 <= intervalTime) {
                            let index = Decorator.DoubleNameClickMap.indexOf(obj);
                            Decorator.DoubleNameClickMap.splice(index, 1);
                            oldFunc.apply(this, args)
                            return;
                        }
                        else {
                            obj.lastTime = time;
                            return;
                        }
                    }
                }
                if (nullObj) {
                    nullObj.name = funcName;
                    nullObj.lastTime = time;
                }
                else {
                    let info = {
                        'name': funcName,
                        'lastTime': time
                    }
                    Decorator.DoubleNameClickMap.push(info);
                }
            }
        }
    }
}